﻿<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../../../assets/docs.css" rel="stylesheet" type="text/css" media="screen" />
<title>MooTools Documentation - Utilities/Cookie</title>
</head>

<body>
  <div id="docs" class="doc">
    <div class="methods">
      <h4 class="menu-item"><a href="#Cookie">Cookie</a></h4>
			<div class="menu-item"><a href="#Cookie-options">Cookie.options</a></div>
			<div class="menu-item"><a href="#Cookie:write">write</a></div>
			<div class="menu-item"><a href="#Cookie:read">read</a></div>
			<div class="menu-item"><a href="#Cookie:dispose">dispose</a></div>
    </div>
    <div class="l-border r-border span-15 content">
				<h1 id="Cookie"><a href="#Cookie">Object: Cookie</a></h1>

<p class="description">Reads and writes a cookie.</p>

<h2 id="Cookie-options"><a href="#Cookie-options">Options:</a></h2>

<ul class="description">
<li>domain   - (<em>string</em>: defaults to false) The domain the cookie belongs to.</li>
<li>path     - (<em>string</em>: defaults to '/') The path the cookie belongs to.</li>
<li>duration - (<em>number</em>: defaults to false) The duration of the cookie (in days) before it expires. If set to false or 0, the cookie will be a session cookie that expires when the browser is closed.</li>
<li>secure   - (<em>boolean</em>: defaults to false) Stored cookie information can be accessed only from a secure environment.</li>
</ul>
<h2 id="Cookie:write"><a href="#Cookie:write">Cookie Method: write</a></h2>

<p class="description">Writes a cookie in the browser.</p>

<h3>Syntax:</h3>

<pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span>key, value<span class="br0">[</span>, options<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>key     - (<em>string</em>) The key (or name) of the cookie.</li>
<li>value   - (<em>string</em>) The value to set. Cannot contain semicolons.</li>
<li>options - (<em>mixed</em>, optional) See <a href="#Cookie">Cookie</a>.</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>object</em>) An object with the options, the key and the value. You can give it as first parameter to <a href="#Cookie:dispose">Cookie.dispose</a>.</li>
</ul>
<h3>Examples:</h3>

<p>Saves the cookie for the duration of the session:</p>

<pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'username'</span>, <span class="st0">'JackBauer'</span><span class="br0">)</span>;
</pre>

<p>Saves the cookie for a day:</p>

<pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'username'</span>, <span class="st0">'JackBauer'</span>, <span class="br0">{</span>duration: <span class="nu0">1</span><span class="br0">}</span><span class="br0">)</span>;
</pre>

<h3>Note:</h3>

<p>In order to share the cookie with pages located in a different path, the <a href="#Cookie-options">Cookie.options.domain</a> value must be set.</p>

<h2 id="Cookie:read"><a href="#Cookie:read">Cookie Method: read</a></h2>

<p class="description">Reads the value of a cookie.</p>

<h3>Syntax:</h3>

<pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="me1">read</span><span class="br0">(</span><span class="kw3">name</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>name - (<em>string</em>) The name of the cookie to read.</li>
</ol>
<h3>Returns:</h3>

<ul>
<li>(<em>mixed</em>) The cookie string value, or null if not found.</li>
</ul>
<h3>Example:</h3>

<pre class="javascript">Cookie.<span class="me1">read</span><span class="br0">(</span><span class="st0">'username'</span><span class="br0">)</span>;
</pre>

<h2 id="Cookie:dispose"><a href="#Cookie:dispose">Cookie Method: dispose</a></h2>

<p class="description">Removes a cookie from the browser.</p>

<h3>Syntax:</h3>

<pre class="javascript"><span class="kw2">var</span> oldCookie = Cookie.<span class="me1">dispose</span><span class="br0">(</span><span class="kw3">name</span><span class="br0">[</span>, options<span class="br0">]</span><span class="br0">)</span>;
</pre>

<h3>Arguments:</h3>

<ol>
<li>name - (<em>string</em>) The name of the cookie to remove or a previously saved Cookie instance.</li>
<li>options - (<em>object</em>, optional) See <a href="#Cookie">Cookie</a>.</li>
</ol>
<h3>Examples:</h3>

<p>Remove a Cookie:</p>

<pre class="javascript">Cookie.<span class="me1">dispose</span><span class="br0">(</span><span class="st0">'username'</span><span class="br0">)</span>; <span class="co1">// Bye-bye JackBauer!</span>
</pre>

<p>Creating a cookie and removing it right away:</p>

<pre class="javascript"><span class="kw2">var</span> myCookie = Cookie.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'username'</span>, <span class="st0">'JackBauer'</span>, <span class="br0">{</span>domain: <span class="st0">'mootools.net'</span><span class="br0">}</span><span class="br0">)</span>;
<span class="kw1">if</span> <span class="br0">(</span>Cookie.<span class="me1">read</span><span class="br0">(</span><span class="st0">'username'</span><span class="br0">)</span> == <span class="st0">'JackBauer'</span><span class="br0">)</span> <span class="br0">{</span> myCookie.<span class="me1">dispose</span><span class="br0">(</span><span class="br0">)</span>; <span class="br0">}</span>
</pre>

<h3>Credits:</h3>

<ul>
<li>Based on the functions by Peter-Paul Koch of <a href="http://www.quirksmode.org/">QuirksMode</a>.</li>
</ul>

    </div>
  </div>
<script type="text/javascript" src="../../../assets/mootools-core-1.3.js"></script>
<script type="text/javascript" src="../../../assets/mootools-more-smoothscroll.js"></script>
<script type="text/javascript" src="../../../assets/doc-assist.js"></script>
</body>
</html>